package com.zito.health.service.impl;

import com.zito.health.dto.*;
import com.zito.health.entity.*;
import com.zito.health.mapper.*;
import com.zito.health.service.LaboratoryService;
import com.zito.health.vo.LabCoordinateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: gxt
 * @time: 2019/11/8 15:33
 */
@Service
public class LaboratoryServiceImpl implements LaboratoryService {

    @Autowired
    private LaboratoryMapper laboratoryMapper;
    @Autowired
    private LaboratoryLinkmanMapper linkmanMapper;

    @Autowired
    private LaboratoryInchargeMapper inchargeMapper;

    @Autowired
    private LaboratorySystemMapper systemMapper;

    @Autowired
    private LaboratoryEquipmentMapper equipmentMapper;

    @Autowired
    private LaboratoryPersonnelMapper personnelMapper;

    @Autowired
    private LaboratoryActivity1Mapper activity1Mapper;

    @Autowired
    private LaboratoryActivity2Mapper activity2Mapper;

    @Autowired
    private LaboratoryPictureMapper pictureMapper;

    @Autowired
    private LaboratoryEquipmentMapper laboratoryEquipmentMapper;

    @Override
    public Laboratory save(Laboratory object) {
        int res = laboratoryMapper.insertSelective(object);
        return object;
    }

    @Override
    public void delete(Integer id) {
        laboratoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Laboratory object) {
        laboratoryMapper.updateByPrimaryKeySelective(object);
    }

    @Override
    public List<Laboratory> query() {
        return laboratoryMapper.selectAll();
    }

    @Override
    public List<LaboratoryDto> queryLaboratory(LaboratoryDto object) {
        return laboratoryMapper.queryLaboratory(object);
    }

    @Override
    public List<Laboratory> query(Laboratory object) {
        return laboratoryMapper.select(object);
    }

    @Override
    public Laboratory queryOne(Laboratory object) {
        return laboratoryMapper.selectOne(object);
    }

    @Override
    public Laboratory queryById(Integer id) {
        Laboratory object = laboratoryMapper.selectByPrimaryKey(id);
        return object;
    }

    @Override
    public LaboratoryDto queryLaboratoryById(Integer id) {
        return laboratoryMapper.queryLaboratoryById(id);
    }

    @Override
    public boolean exists(Integer id) {
        return laboratoryMapper.existsWithPrimaryKey(id);
    }


    @Override
    @Transactional
    public int deleteById(Integer id) {
        //删除实验室信息
        laboratoryMapper.deleteByPrimaryKey(id);
        //删除联系人信息
        LaboratoryLinkman laboratoryLinkman=new LaboratoryLinkman();
        laboratoryLinkman.setLaboratoryId(id);
        linkmanMapper.delete(laboratoryLinkman);
        //删除负责人信息
        LaboratoryIncharge laboratoryIncharge = new LaboratoryIncharge();
        laboratoryIncharge.setLaboratoryId(id);
        inchargeMapper.delete(laboratoryIncharge);
        //删除制度信息
        LaboratorySystem laboratorySystem =new LaboratorySystem();
        laboratorySystem.setLaboratoryId(id);
        systemMapper.delete(laboratorySystem);
        //删除人员信息
        LaboratoryPersonnel laboratoryPersonnel=new LaboratoryPersonnel();
        laboratoryPersonnel.setLaboratoryId(id);
        personnelMapper.delete(laboratoryPersonnel);
        //删除设备信息
        LaboratoryEquipment laboratoryEquipment=new LaboratoryEquipment();
        laboratoryEquipment.setLaboratoryId(id);
        equipmentMapper.delete(laboratoryEquipment);
        //删除活动一信息
        LaboratoryActivity1 laboratoryActivity1=new LaboratoryActivity1();
        laboratoryActivity1.setLaboratoryId(id);
        activity1Mapper.delete(laboratoryActivity1);
        //删除活动二信息
        LaboratoryActivity2 laboratoryActivity2=new LaboratoryActivity2();
        laboratoryActivity2.setLaboratoryId(id);
        activity2Mapper.delete(laboratoryActivity2);
        //删除附件信息
        LaboratoryPicture laboratoryPicture=new LaboratoryPicture();
        laboratoryPicture.setLaboratoryId(id);
        pictureMapper.delete(laboratoryPicture);
        return 0;
    }

    @Override
    public LaboratoryPrint1Dto print1(Integer id) {
        LaboratoryPrint1Dto laboratoryPrint1Dto = laboratoryMapper.print1(id);
        LaboratoryEquipment laboratoryEquipment= new LaboratoryEquipment();
        laboratoryEquipment.setLaboratoryId(id);
        List<LaboratoryEquipment> laboratoryEquipmentList= laboratoryEquipmentMapper.select(laboratoryEquipment);
        laboratoryPrint1Dto.setLaboratoryEquipments(laboratoryEquipmentList);
        return laboratoryPrint1Dto;
    }
    @Override
    public LaboratoryPrint2Dto print2(Integer id) {
        LaboratoryPrint2Dto laboratoryPrint2Dto = new LaboratoryPrint2Dto();
        Laboratory laboratory = laboratoryMapper.selectByPrimaryKey(id);
        if (laboratory != null) {
            // 活动一
            LaboratoryActivity1 labActivity1 = new LaboratoryActivity1();
            labActivity1.setLaboratoryId(id);
            List<LaboratoryActivity1> activity1s = activity1Mapper.select(labActivity1);

            List<LaboratoryActivity1Dto> activity1 =new ArrayList<LaboratoryActivity1Dto>();
            List<LaboratoryActivity1Dto> activity2 =new ArrayList<LaboratoryActivity1Dto>();
            List<LaboratoryActivity1Dto> activity3 =new ArrayList<LaboratoryActivity1Dto>();
            if (activity1s != null && activity1s.size() > 0) {
                for(LaboratoryActivity1 activitys1 :activity1s){
                    LaboratoryActivity1Dto activity1Dto = new LaboratoryActivity1Dto();
                    activity1Dto.setIntegerOne(laboratory.getBiosafety());
                    if(activitys1.getItem()==1){
                        BeanUtils.copyProperties(activitys1,activity1Dto);
                        activity1.add(activity1Dto);
                    }
                    if(activitys1.getItem()==2){
                        BeanUtils.copyProperties(activitys1,activity1Dto);
                        activity2.add(activity1Dto);
                    }
                    if(activitys1.getItem()==3){
                        BeanUtils.copyProperties(activitys1,activity1Dto);
                        activity3.add(activity1Dto);
                    }
                }
            }
            laboratoryPrint2Dto.setActivity1(activity1);
            laboratoryPrint2Dto.setActivity2(activity2);
            laboratoryPrint2Dto.setActivity3(activity3);
            // 活动二
            LaboratoryActivity2 labActivity2 = new LaboratoryActivity2();
            labActivity2.setLaboratoryId(id);
            List<LaboratoryActivity2> activity2s = activity2Mapper.select(labActivity2);

            List<LaboratoryActivity2Dto> activity4 =new ArrayList<LaboratoryActivity2Dto>();
            List<LaboratoryActivity2Dto> activity5 =new ArrayList<LaboratoryActivity2Dto>();
            if (activity2s != null && activity2s.size() > 0) {
                for(LaboratoryActivity2 activitys2 :activity2s){
                    LaboratoryActivity2Dto activity2Dto = new LaboratoryActivity2Dto();
                    activity2Dto.setIntegerOne(laboratory.getBiosafety());
                    if(activitys2.getItem()==1){
                        BeanUtils.copyProperties(activitys2,activity2Dto);
                        activity4.add(activity2Dto);
                    }
                    if(activitys2.getItem()==2){
                        BeanUtils.copyProperties(activitys2,activity2Dto);
                        activity5.add(activity2Dto);
                    }
                }
            }
            laboratoryPrint2Dto.setActivity4(activity4);
            laboratoryPrint2Dto.setActivity5(activity5);
        }
        return laboratoryPrint2Dto;
    }

    @Override
    public List<LabCoordinateVo> getCoordinates(String labName) {

        return laboratoryMapper.getCoordinates(labName);
    }

    @Override
    public Integer getLabNum() {
        return laboratoryMapper.getLabNum();
    }

    @Override
    public LaboratoryVo getById(Integer id) {
        return laboratoryMapper.getById(id);
    }

    @Override
    public List<LaboratoryDto> list4Archives(LaboratoryDto laboratoryDto) {
        return laboratoryMapper.list4Archives(laboratoryDto);
    }

    @Override
    public String canSubmit(Laboratory laboratory) {
        Integer id=laboratory.getId();

        //实验室基本信息（其实是联系人信息）
        LaboratoryLinkman laboratoryLinkman=new LaboratoryLinkman();
        laboratoryLinkman.setLaboratoryId(id);
        laboratoryLinkman =linkmanMapper.selectOne(laboratoryLinkman);
        if(laboratoryLinkman==null){
            return "您未填写实验室基本信息";
        }
        
        //实验室人员
        LaboratoryPersonnel laboratoryPersonnel=new LaboratoryPersonnel();
        laboratoryPersonnel.setLaboratoryId(id);
        List<LaboratoryPersonnel>  laboratoryPersonnels=personnelMapper.select(laboratoryPersonnel);
        if(laboratoryPersonnels==null||laboratoryPersonnels.size()==0){
            return "您未填写实验室人员";
        }
        
        //实验室负责人
        LaboratoryIncharge laboratoryIncharge = new LaboratoryIncharge();
        laboratoryIncharge.setLaboratoryId(id);
        laboratoryIncharge =inchargeMapper.selectOne(laboratoryIncharge);
        if(laboratoryIncharge==null){
            return "您未填写实验室负责人";
        }
        //基础设施
        Laboratory l = laboratoryMapper.selectByPrimaryKey(id);
        if(l==null || l.getWater()==0){//
            return "您未填写基础设施";
        }
        //生物安全及防护设备
        LaboratoryEquipment laboratoryEquipment=new LaboratoryEquipment();
        laboratoryEquipment.setLaboratoryId(id);
        List<LaboratoryEquipment> laboratoryEquipments = equipmentMapper.select(laboratoryEquipment);
        if(laboratoryEquipments==null||laboratoryEquipments.size()==0){
            return "您未填写生物安全及防护设备";
        }
        //制度
        LaboratorySystem laboratorySystem =new LaboratorySystem();
        laboratorySystem.setLaboratoryId(id);
        List<LaboratorySystem> laboratorySystems=systemMapper.select(laboratorySystem);
        if(laboratorySystems == null||laboratorySystems.size()==0){
            return "您未填写制度";
        }

        //实验室活动基本情况一
        LaboratoryActivity1 laboratoryActivity1=new LaboratoryActivity1();
        laboratoryActivity1.setLaboratoryId(id);
        List<LaboratoryActivity1> laboratoryActivity1s =activity1Mapper.select(laboratoryActivity1);
        if(laboratoryActivity1s==null||laboratoryActivity1s.size()==0){
            return "您未填写实验室活动基本情况一";
        }
        //实验室活动基本情况二
        LaboratoryActivity2 laboratoryActivity2=new LaboratoryActivity2();
        laboratoryActivity2.setLaboratoryId(id);
        List<LaboratoryActivity2> laboratoryActivity2s =activity2Mapper.select(laboratoryActivity2);
        if(laboratoryActivity2s==null||laboratoryActivity2s.size()==0){
            return "您未填写验室活动基本情况二";
        }
        //附件
        LaboratoryPicture laboratoryPicture=new LaboratoryPicture();
        laboratoryPicture.setLaboratoryId(id);
        List<LaboratoryPicture> laboratoryPictures =pictureMapper.select(laboratoryPicture);
        if(laboratoryPictures==null||laboratoryPictures.size()==0){
            return "您未填写附件";
        }

        return "ok";
    }

}
